home *** CD-ROM | disk | FTP | other *** search
-
- /* standard headers */
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
-
- /* system headers */
- #include <Gestalt.h>
- #include <LowMem.h>
- #include <Strings.h>
- #include <TextUtils.h>
- #include <CodeFragments.h>
- #include <Resources.h>
- #include <MixedMode.h>
-
- /* compiler headers */
- #include <A4Stuff.h>
- #include <DropInCompiler.h>
-
- /* project headers */
- #include "ToolFrontEnd.h"
- #include "ToolServer.h"
- #include "FullPath.h"
-
-
-
- /* prototypes of local functions */
- static OSErr Compile(ToolFrontEndStatus* status);
- static OSErr ProcessFile(ToolFrontEndStatus* status, ToolFrontEndPref **prefs,
- short currentExtension);
-
-
- // mixed mode info for include file scanners
- enum
- {
- uppScannerEntryPointInfo = kPascalStackBased
- | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
- | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ToolFrontEndStatus*)))
- };
-
-
- /*
- * main - main entry-point for ToolFrontEnd Tool
- *
- */
-
- pascal short main(CompilerParameterBlockPtr cpb)
- {
- short result;
- ToolFrontEndStatus* status;
- OSErr err;
-
- /* set up global world (68K only) */
- EnterCodeResource();
-
- result = noErr;
-
- /* dispatch on compiler request */
- switch (cpb->request)
- {
- case reqInitCompiler:
- /* compiler has just been loaded into memory */
- break;
-
- case reqTermCompiler:
- /* compiler is about to be unloaded from memory */
- break;
-
- case reqCompile:
- /* compile a source file */
- status = (ToolFrontEndStatus*)NewPtrClear(sizeof(ToolFrontEndStatus));
- err = MemError();
- if (err == noErr)
- {
- int i;
- status->cpb = cpb;
- err = Compile(status);
- for (i = 0; i < status->numFolderPaths; i++)
- DisposeHandle(status->folderPaths[i].path);
- DisposePtr((Ptr)status);
- }
- result = (err == noErr);
- break;
-
- default:
- result = paramErr;
- break;
- }
-
- /* tear down global world (68K only) */
- ExitCodeResource();
-
- /* return result code */
- return (result);
- }
-
-
- static OSErr Compile(ToolFrontEndStatus* status)
- {
- Handle prefsHand;
- ToolFrontEndPref** prefs;
- OSErr err;
- short currentExtension;
-
- /* Load in our prefs. */
- err = CWCompGetPreferences(status->cpb, kToolFrontEndPanelName, &prefsHand);
- if (err != noErr) return err;
- prefs = (ToolFrontEndPrefHandle)prefsHand;
-
- status->linecount = 0;
-
- // find the extension of this file
- {
- Str15 extension;
- int i;
- Byte state;
-
- for (i = status->cpb->sourcefile.name[0]; i > 0 && status->cpb->sourcefile.name[i] != '.'; i--)
- ;
- if (i < 0) return paramErr;
- extension[0] = status->cpb->sourcefile.name[0] - (i - 1);
- BlockMoveData(status->cpb->sourcefile.name + i, extension + 1, extension[0]);
-
- state = HGetState((Handle)prefs);
- HLock((Handle)prefs);
- for (i = 0, currentExtension = -1; i < (*prefs)->numExtensions; i++)
- {
- if (EqualString(extension, (*prefs)->extensions[i].extension, false, true))
- {
- currentExtension = i;
- break;
- }
- }
- HSetState((Handle)prefs, state);
- if (currentExtension == -1) return paramErr;
- }
-
- // process include files
- if ((*prefs)->extensions[currentExtension].scanner[0] != 0)
- {
- err = ProcessFile(status, prefs, currentExtension);
- }
-
- // now build the ToolServer command
- {
- Handle commandHandle = NewHandle(0);
- AEDesc command;
- CStringHandle output = NULL, diagnostic = NULL;
- Str31 nameStem;
-
- // parameterize the ToolServer command line
-
- if (status->numFolderPaths > 0)
- {
- // add the IncludeFiles variable
- int i;
-
- PtrAndHand("Set IncludeFiles \'", commandHandle, sizeof("Set IncludeFiles \'") - 1);
- for (i = 0; i < status->numFolderPaths; i++)
- {
- char space = ' ', quote = '\"';
- Byte state;
-
- state = HGetState((Handle)prefs);
- HLock((Handle)prefs);
- PtrAndHand((*prefs)->extensions[currentExtension].includeFileArg + 1,
- commandHandle, (*prefs)->extensions[currentExtension].includeFileArg[0]);
- HSetState((Handle)prefs, state);
- PtrAndHand(&space, commandHandle, 1);
- PtrAndHand("e, commandHandle, 1);
- HandAndHand(status->folderPaths[i].path, commandHandle);
- PtrAndHand("e, commandHandle, 1);
- PtrAndHand(&space, commandHandle, 1);
- }
- PtrAndHand("\'; ", commandHandle, sizeof("\'; ") - 1);
- }
-
- // add the SourceFile variable -- XXX -- if not saved, use temp file instead!
- {
- Handle fileName;
- short fileNameSize;
-
- PtrAndHand("Set SourceFile \'", commandHandle, sizeof("Set SourceFile \'") - 1);
- err = FSpGetFullPath(&status->cpb->sourcefile, &fileNameSize, &fileName);
- if (err == noErr)
- {
- HandAndHand(fileName, commandHandle);
- DisposeHandle(fileName);
- PtrAndHand("\'; ", commandHandle, sizeof("\'; ") - 1);
- }
- }
-
- // add the SourceFileStem variable
- if (err == noErr)
- {
- int i;
-
- BlockMoveData(status->cpb->sourcefile.name, nameStem, status->cpb->sourcefile.name[0] + 1);
- for (i = nameStem[0]; i > 0; i--)
- {
- if (nameStem[i] == '.')
- {
- nameStem[0] = i - 1;
- break;
- }
- }
-
- PtrAndHand("Set SourceFileStem \'", commandHandle, sizeof("Set SourceFileStem \'") - 1);
- PtrAndHand(nameStem + 1, commandHandle, nameStem[0]);
- PtrAndHand("\'; ", commandHandle, sizeof("\'; ") - 1);
- }
-
- // add the Directory command to go to the project folder
- if (err == noErr)
- {
- short dirNameLength;
- Handle dirName;
-
- err = GetFullPath(status->cpb->targetfile.vRefNum, status->cpb->targetfile.parID, NULL, &dirNameLength, &dirName);
-
- if (err == noErr)
- {
- PtrAndHand("Directory \'", commandHandle, sizeof("Directory \'") - 1);
- HandAndHand(dirName, commandHandle);
- PtrAndHand("\'; ", commandHandle, sizeof("\'; ") - 1);
- DisposeHandle(dirName);
- }
- }
-
- // add the Execute command for the script include file, if any
- if ((err == noErr) && (*prefs)->scriptIncludeFile[0] != 0)
- {
- Str31 includeFileName;
- FSSpec includeSpec;
- Boolean alreadyincluded;
- Handle hand = NULL;
- long size;
-
- Byte state = HGetState((Handle)prefs);
- HLock((Handle)prefs);
- BlockMoveData((*prefs)->scriptIncludeFile, includeFileName,
- (*prefs)->scriptIncludeFile[0] + 1);
- HSetState((Handle)prefs, state);
-
- err = CWCompFindIncludeFile(status->cpb, includeFileName, true, &hand, &size,
- &includeSpec, &alreadyincluded, false);
- if (err != noErr)
- {
- CWCompOSErrorMessage(status->cpb, "The include file could not be found.", err);
- }
- else
- {
- short incNameLength;
- Handle incName;
-
- if (hand != NULL)
- {
- // XXX -- file is open in IDE window -- use temp file eventually
- CWCompErrorMessage(status->cpb, "The include file is open. Please close it first.");
- err = paramErr;
- }
- else
- {
- err = FSpGetFullPath(&includeSpec, &incNameLength, &incName);
- if (err != noErr)
- {
- CWCompOSErrorMessage(status->cpb, "Error getting full path name from include file.", err);
- }
- else
- {
- PtrAndHand("Execute \'", commandHandle, sizeof("Execute \'") - 1);
- HandAndHand(incName, commandHandle);
- PtrAndHand("\'; ", commandHandle, sizeof("\'; ") - 1);
- DisposeHandle(incName);
- }
- }
- }
- }
-
- // add the template command line from prefs
- if (err == noErr)
- {
- Byte state = HGetState((Handle)prefs);
- HLock((Handle)prefs);
- PtrAndHand((*prefs)->extensions[currentExtension].commandTemplate + 1, commandHandle,
- (*prefs)->extensions[currentExtension].commandTemplate[0]);
- HSetState((Handle)prefs, state);
- }
-
- // make the ToolServer command
- if (err == noErr)
- {
- HLock(commandHandle);
- command.descriptorType = typeNull;
- command.dataHandle = NULL;
- err = AECreateList(NULL, 0, false, &command);
- if (err == noErr)
- err = AEPutPtr(&command, 0, typeChar, *commandHandle, GetHandleSize(commandHandle));
- }
- DisposeHandle(commandHandle);
-
- if (err == noErr)
- {
- // XXX create a progress indicator window
-
- // send the ToolServer command -- XXX with progress indicator idleProc
- err = ToolServerCommand(&command, &output, &diagnostic, NULL, NULL);
- AEDisposeDesc(&command);
-
- // XXX remove the progress indicator
-
- // show number of lines processed
- CWCompDisplayLines(status->cpb, status->linecount);
-
- // display any diagnostic result
- if (err != noErr)
- {
- if (diagnostic != NULL)
- {
- char null = 0;
- PtrAndHand(&null, (Handle)diagnostic, 1);
- HLock((Handle)diagnostic);
- CWCompErrorMessage(status->cpb, (const char*)*diagnostic);
- }
- else
- {
- CWCompOSErrorMessage(status->cpb, "ToolServer error. ", err);
- }
- }
- #if 0
- else
- {
- // XXX test code for a future version
- FSSpec testSpec;
- unsigned long time;
- Handle path;
- short len;
- char c;
-
- testSpec.vRefNum = status->cpb->targetfile.vRefNum;
- testSpec.parID = status->cpb->targetfile.parID;
- BlockMoveData(nameStem, testSpec.name, nameStem[0] + 1);
- BlockMoveData(".xh", testSpec.name + nameStem[0] + 1, 3);
- testSpec.name[0] += 3;
- err = FSpGetFullPath(&testSpec, &len, &path);
- if (err != noErr) DebugStr("\pgoldarnit!");
- c = 0;
- PtrAndHand(&c, path, 1);
- HLock(path);
- err = CWCompGetPrecompiledHeaderSpec(status->cpb, &testSpec, (char*)*path);
- if (err != noErr) DebugStr("\pfug!");
- GetDateTime(&time);
- CWCompSetModDate(status->cpb, &testSpec, time);
- //if (err != noErr) DebugStr("\pheck!");
- }
- #endif
- }
- if (output != NULL) DisposeHandle((Handle)output);
- if (diagnostic != NULL) DisposeHandle((Handle)diagnostic);
- }
-
- return err;
- }
-
-
- static OSErr ProcessFile(ToolFrontEndStatus* status, ToolFrontEndPref** prefs,
- short currentExtension)
- {
- OSErr err;
- FCBPBRec pb;
- FSSpec spec;
- CInfoPBRec ci;
- #if GENERATINGCFM
- long response;
- #endif
-
- // find the scanner folder and file
- pb.ioFCBIndx = 0; // use ioRefNum
- pb.ioRefNum = LMGetCurApRefNum();
- pb.ioNamePtr = NULL;
- err = PBGetFCBInfoSync(&pb);
- if (err != noErr) return err;
- GetIndString(spec.name, 129, 1); // CodeWarrior Plugins
- ci.dirInfo.ioNamePtr = spec.name;
- ci.dirInfo.ioVRefNum = pb.ioFCBVRefNum;
- ci.dirInfo.ioDrDirID = pb.ioFCBParID;
- ci.dirInfo.ioFDirIndex = 0;
- err = PBGetCatInfoSync(&ci);
- if (err != noErr) return err;
- GetIndString(spec.name, 129, 2); // Include Scanners
- ci.dirInfo.ioNamePtr = spec.name;
- ci.dirInfo.ioVRefNum = pb.ioFCBVRefNum;
- ci.dirInfo.ioDrDirID = ci.dirInfo.ioDrDirID;
- ci.dirInfo.ioFDirIndex = 0;
- err = PBGetCatInfoSync(&ci);
- if (err != noErr) return err;
- spec.vRefNum = pb.ioFCBVRefNum;
- spec.parID = ci.dirInfo.ioDrDirID;
- BlockMoveData((*prefs)->extensions[currentExtension].scanner, spec.name,
- (*prefs)->extensions[currentExtension].scanner[0] + 1);
-
- #if GENERATINGCFM
- // if on PowerPC...
- err = Gestalt(gestaltSysArchitecture, &response);
- if (err == noErr && response == gestaltPowerPC)
- {
- // load code fragment
- CFragConnectionID connID;
- Str255 errName;
- UniversalProcPtr newRoutine = 0;
- Ptr mainAddr;
-
- err = GetDiskFragment(&spec, 0, kWholeFork, NULL, kLoadNewCopy, &connID, &mainAddr,
- errName);
- if (err != noErr) return err;
-
- // call main entry point
- newRoutine = NewRoutineDescriptor((ProcPtr)mainAddr, uppScannerEntryPointInfo, GetCurrentArchitecture());
- if (newRoutine == NULL)
- {
- err = MemError();
- CloseConnection(&connID);
- return err;
- }
- err = CallUniversalProc(newRoutine, uppScannerEntryPointInfo, status);
-
- // unload code fragment
- CloseConnection(&connID);
- }
- else // 68K
- #endif
- {
- Handle h;
- pascal OSErr (*f)(ToolFrontEndStatus* status);
- short refNum;
-
- // open resource file
- refNum = FSpOpenResFile(&spec, fsRdPerm);
- if (refNum < 0) return ResError();
-
- // load code resource
- h = Get1Resource('Scan', 128);
- if (h == 0)
- {
- CloseResFile(refNum);
- return resNotFound;
- }
-
- // call main entry point
- HNoPurge(h);
- HLock(h);
- f = (pascal OSErr (*)(ToolFrontEndStatus* status))(*h);
- err = (*f)(status);
-
- // close resource file
- CloseResFile(refNum);
- }
-
- if (err != noErr)
- {
- Str31 s;
- NumToString(err, s);
- p2cstr(s);
- CWCompErrorMessage(status->cpb, (char*)s);
- }
-
- return err;
- }
-